package com.findme.unvelouse.map;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;

import com.findme.unvelouse.VelouseGoogleMapsActivity;
import com.google.android.maps.ItemizedOverlay;

public class VeloStationItemizedOverlay extends ItemizedOverlay<VeloStationOverlayItem> {

	private final ArrayList<VeloStationOverlayItem> mVeloStationOverlays = new ArrayList<VeloStationOverlayItem>();

	private final Context mContext;

	public VeloStationItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	public void addOverlay(VeloStationOverlayItem overlay) {
		mVeloStationOverlays.add(overlay);
		populate();
	}

	@Override
	protected VeloStationOverlayItem createItem(int arg0) {
		return mVeloStationOverlays.get(arg0);
	}

	@Override
	public int size() {
		return mVeloStationOverlays.size();
	}

	@Override
	protected boolean onTap(final int index) {
		final VeloStationOverlayItem item = mVeloStationOverlays.get(index);
		final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());

		if (!item.getVeloStation().isFavori()) {
			// already favoris
			dialog.setNeutralButton("Ajouter aux favoris", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialogInterface, int which) {
					// add to favorite
					((VelouseGoogleMapsActivity) mContext).saveFavoris(item.getVeloStation(), true);
				}
			});
		} else {
			// not favoris
			dialog.setNeutralButton("Retirer des favoris", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialogInterface, int which) {
					// add to favorite
					((VelouseGoogleMapsActivity) mContext).saveFavoris(item.getVeloStation(), false);
				}
			});
		}
		dialog.show();
		return true;
	}
}
